Passed
Push — development ( e131c7...d0a4d7 )
by Peter
05:27 queued 13s
created

UsersService.updateTerms   A

Complexity

Conditions 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 10
dl 0
loc 12
ccs 4
cts 5
cp 0.8
crap 2.032
rs 9.9
c 0
b 0
f 0
1 2
import { Injectable } from '@nestjs/common';
2 2
import { InjectRepository } from '@nestjs/typeorm';
3 2
import { Repository } from 'typeorm';
4 2
import { User } from './entities/user.entity';
5 2
import { NotFoundException } from '@nestjs/common';
0 ignored issues
show
introduced by
'@nestjs/common' import is duplicated.
Loading history...
6
import { UpdateUserDto } from './dto/update-user.dto/update-user.dto';
7
import { AdjustFundsDto } from './dto/update-user.dto/adjust-funds.dto';
8
9
@Injectable()
10 2
export class UsersService {
11
  constructor(
12
    @InjectRepository(User)
13 2
    private userRepository: Repository<User>,
14
  ) {}
15
16
  async updateTerms(githubId: string, hasAcceptedTerms: boolean): Promise<User> {
17 1
    const user = await this.userRepository.findOne({
18
      where: { githubId },
19
    });
20
21 1
    if (!user) {
22
      throw new NotFoundException('User not found');
23
    }
24
25 1
    user.hasAcceptedTerms = hasAcceptedTerms;
26 1
    return this.userRepository.save(user);
27
  }
28
  // Find all customers
29
  async findAll(): Promise<User[]> {
30
    return this.userRepository.find();
31
  }
32
  // Find a customer by ID
33
  async findById(githubId: string): Promise<User> {
34
    const user = await this.userRepository.findOne({ where: { githubId } });
35 1
    if (!user) {
36
      throw new NotFoundException('User not found');
37
    }
38
    return user;
39
  }
40
  // Update customer fields
41
  async update(githubId: string, updateUserDto: UpdateUserDto): Promise<User> {
42
    const user = await this.findById(githubId);
43
44
    return this.userRepository.save({ ...user, ...updateUserDto });
45
  }
46
47
  async adjustFunds(githubId: string, adjustFundsDto: AdjustFundsDto): Promise<User> {
48
    const user = await this.userRepository.findOneBy({ githubId });
49
50 1
    if (!user) {
51
      throw new NotFoundException(`User with GitHub ID ${githubId} not found.`);
52
    }
53
54
    // Update balance and/or isMonthlyPayment only if provided
55 1
    if (adjustFundsDto.balance !== undefined) {
56
      user.balance = adjustFundsDto.balance;
57
    }
58 1
    if (adjustFundsDto.isMonthlyPayment !== undefined) {
59
      user.isMonthlyPayment = adjustFundsDto.isMonthlyPayment;
60
    }
61
62
    return this.userRepository.save(user);
63
  }
64
}
65